package com.gitee.ricesugar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import static com.gitee.ricesugar.admin.AdminPanel.createAdminPanel;
import static com.gitee.ricesugar.book.BookPanel.createBookPanel;
import static com.gitee.ricesugar.borrow.BorrowPanel.createBorrowPanel;
import static com.gitee.ricesugar.user.UserPanel.createUserPanel;

public class Main {
    private final JFrame mainFrame;
    private final JPanel mainPanel;
    private final JPanel userPanel;
    private final JPanel adminPanel;
    private final JPanel bookPanel;
    private final JPanel borrowPanel;

    public Main() throws IOException {
        mainFrame = new JFrame("图书管理系统");
        mainFrame.setSize(800, 600);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - mainFrame.getWidth()) / 2;
        int y = (screenSize.height - mainFrame.getHeight()) / 2;
        mainFrame.setLocation(x, y);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        createToolBar();
        userPanel = createUserPanel();
        adminPanel = createAdminPanel();
        bookPanel = createBookPanel();
        borrowPanel = createBorrowPanel();

        mainPanel = new JPanel(new CardLayout());

        mainPanel.add(userPanel, "User");
        mainPanel.add(adminPanel, "Admin");
        mainPanel.add(bookPanel, "Book");
        mainPanel.add(borrowPanel, "Borrow");

        showPanel("User");

        mainFrame.getContentPane().add(mainPanel);
        mainFrame.setVisible(true);
    }

    private void createToolBar() {
        JPanel toolBarPanel = new JPanel();
        toolBarPanel.setLayout(new GridLayout(1, 3));
        JButton userButton = new JButton("用户管理");
        JButton adminButton = new JButton("管理员管理");
        JButton bookButton = new JButton("图书管理");
        JButton borrowButton = new JButton("借阅管理");
        userButton.setPreferredSize(new Dimension(100, 50));

        userButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("User");
            }
        });

        adminButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("Admin");
            }
        });

        bookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("Book");
            }
        });
        borrowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("Borrow");
            }
        });


        toolBarPanel.add(userButton);
        toolBarPanel.add(adminButton);
        toolBarPanel.add(bookButton);
        toolBarPanel.add(borrowButton);

        mainFrame.add(toolBarPanel, BorderLayout.NORTH);
    }

    private void showPanel(String panelName) {
        CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
        cardLayout.show(mainPanel, panelName);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Main();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
